<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索框区域 -->
      <el-row type="flex" justify="" :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="7">
          <div>
            <el-input
              placeholder="请输入搜索内容"
              v-model="queryInfo.query"
              clearable
              @clear="getGoodList()"
              class="input-with-select"
            >
              <!-- 搜索按钮 -->
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getGoodList()"
              ></el-button>
            </el-input>
          </div>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="3">
          <el-button type="primary" @click="toAddGood()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格区域 -->
      <el-table :data="goodsList" stripe border="" style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="700">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" align="center">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" align="center">
        </el-table-column>
        <el-table-column label="创建时间" width="170" align="center">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <div>
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="toEditTheGood(scope.row)"
              ></el-button>

              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteTheGood(scope.row)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 商品分页模块 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
 

<script>
export default {
  data() {
    return {
      //存放商品列表的
      queryInfo: {
        query: "", //查询参数可以为空
        pagenum: 1, //当前页码不能为空
        pagesize: 10, //每页显示条数不能为空
      },
      //存放商品列表数据
      goodsList: [],
      //存放总共的数据条数
      total: 0,
    };
  },
  methods: {
    //获取商品列表的函数
    async getGoodList() {
      const { data: res } = await this.$http.get(`goods`, {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.meta.status == 200) {
        //获取列表成功 进行赋值
        this.goodsList = res.data.goods;
        this.total = res.data.total;
      } else {
        //获取列表失败
        this.$message.error("获取商品失败，请检查网络");
      }
    },
    //商品分页器的相关操作
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodList();
    },
    handleCurrentChange(val) {
      //页码发生改变的时候
      this.queryInfo.pagenum = val;
      this.getGoodList();
    },
    async deleteTheGood(good) {
      //const { data: res } = await this.$http.delete(`goods/${good.goods_id}`);
      this.$confirm("此操作将永久删除商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            `goods/${good.goods_id}`
          );
          if (res.meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getGoodList();
          } else {
            this.$message({
              type: "warning",
              message: "删除失败，请检查网络",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编程式导航跳转到添加商品页面
    toAddGood() {
      this.$router.push("/goods/add");
    },
  },
  created() {
    //页面一上来就获取商品列表
    this.getGoodList();
  },
};
</script>

<style lang="less" scoped>
</style>